.class public Lcom/kwad/sdk/api/loader/DFileLock;
.super Ljava/lang/Object;


# annotations
.annotation system Ldalvik/annotation/MemberClasses;
    value = {
        Lcom/kwad/sdk/api/loader/DFileLock$LockListener;,
        Lcom/kwad/sdk/api/loader/DFileLock$LockCallback;
    }
.end annotation


# static fields
.field private static instance:Lcom/kwad/sdk/api/loader/DFileLock; = null

.field private static final pidFile:Ljava/lang/String; = "locfile"


# direct methods
.method static constructor <clinit>()V
    .locals 1

    new-instance v0, Lcom/kwad/sdk/api/loader/DFileLock;

    invoke-direct {v0}, Lcom/kwad/sdk/api/loader/DFileLock;-><init>()V

    sput-object v0, Lcom/kwad/sdk/api/loader/DFileLock;->instance:Lcom/kwad/sdk/api/loader/DFileLock;

    return-void
.end method

.method private constructor <init>()V
    .locals 0

    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    return-void
.end method

.method private lockFile(Ljava/io/File;ZLcom/kwad/sdk/api/loader/DFileLock$LockCallback;)V
    .locals 2

    const/4 v0, 0x0

    :try_start_0
    new-instance v1, Ljava/io/FileOutputStream;

    invoke-direct {v1, p1}, Ljava/io/FileOutputStream;-><init>(Ljava/io/File;)V

    invoke-virtual {v1}, Ljava/io/FileOutputStream;->getChannel()Ljava/nio/channels/FileChannel;

    move-result-object p1

    if-eqz p2, :cond_0

    invoke-virtual {p1}, Ljava/nio/channels/FileChannel;->tryLock()Ljava/nio/channels/FileLock;

    move-result-object p1

    goto :goto_0

    :cond_0
    invoke-virtual {p1}, Ljava/nio/channels/FileChannel;->lock()Ljava/nio/channels/FileLock;

    move-result-object p1
    :try_end_0
    .catch Ljava/io/FileNotFoundException; {:try_start_0 .. :try_end_0} :catch_1
    .catch Ljava/io/IOException; {:try_start_0 .. :try_end_0} :catch_0
    .catchall {:try_start_0 .. :try_end_0} :catchall_0

    :goto_0
    if-eqz p3, :cond_1

    invoke-interface {p3, p1}, Lcom/kwad/sdk/api/loader/DFileLock$LockCallback;->run(Ljava/nio/channels/FileLock;)V

    goto :goto_2

    :catchall_0
    move-exception p1

    goto :goto_3

    :catch_0
    move-exception p1

    :try_start_1
    invoke-virtual {p1}, Ljava/io/IOException;->printStackTrace()V

    if-eqz p3, :cond_1

    goto :goto_1

    :catch_1
    move-exception p1

    invoke-virtual {p1}, Ljava/io/FileNotFoundException;->printStackTrace()V
    :try_end_1
    .catchall {:try_start_1 .. :try_end_1} :catchall_0

    if-eqz p3, :cond_1

    :goto_1
    invoke-interface {p3, v0}, Lcom/kwad/sdk/api/loader/DFileLock$LockCallback;->run(Ljava/nio/channels/FileLock;)V

    :cond_1
    :goto_2
    return-void

    :goto_3
    if-eqz p3, :cond_2

    invoke-interface {p3, v0}, Lcom/kwad/sdk/api/loader/DFileLock$LockCallback;->run(Ljava/nio/channels/FileLock;)V

    :cond_2
    throw p1
.end method


# virtual methods
.method public getInstance()Lcom/kwad/sdk/api/loader/DFileLock;
    .locals 1

    sget-object v0, Lcom/kwad/sdk/api/loader/DFileLock;->instance:Lcom/kwad/sdk/api/loader/DFileLock;

    return-object v0
.end method

.method public tryLockFile(Landroid/content/Context;Lcom/kwad/sdk/api/loader/DFileLock$LockListener;)V
    .locals 2

    new-instance v0, Ljava/io/File;

    invoke-virtual {p1}, Landroid/content/Context;->getApplicationInfo()Landroid/content/pm/ApplicationInfo;

    move-result-object p1

    iget-object p1, p1, Landroid/content/pm/ApplicationInfo;->dataDir:Ljava/lang/String;

    const-string v1, "locfile"

    invoke-direct {v0, p1, v1}, Ljava/io/File;-><init>(Ljava/lang/String;Ljava/lang/String;)V

    invoke-virtual {v0}, Ljava/io/File;->exists()Z

    move-result p1

    const/4 v1, 0x1

    if-nez p1, :cond_1

    invoke-interface {p2, v1}, Lcom/kwad/sdk/api/loader/DFileLock$LockListener;->onComplete(Z)V

    :try_start_0
    invoke-virtual {v0}, Ljava/io/File;->createNewFile()Z

    move-result p1

    if-eqz p1, :cond_0

    const/4 p1, 0x0

    new-instance p2, Lcom/kwad/sdk/api/loader/DFileLock$1;

    invoke-direct {p2, p0}, Lcom/kwad/sdk/api/loader/DFileLock$1;-><init>(Lcom/kwad/sdk/api/loader/DFileLock;)V

    invoke-direct {p0, v0, p1, p2}, Lcom/kwad/sdk/api/loader/DFileLock;->lockFile(Ljava/io/File;ZLcom/kwad/sdk/api/loader/DFileLock$LockCallback;)V
    :try_end_0
    .catch Ljava/io/IOException; {:try_start_0 .. :try_end_0} :catch_0

    goto :goto_0

    :catch_0
    move-exception p1

    invoke-virtual {p1}, Ljava/io/IOException;->printStackTrace()V

    :cond_0
    :goto_0
    return-void

    :cond_1
    new-instance p1, Lcom/kwad/sdk/api/loader/DFileLock$2;

    invoke-direct {p1, p0, p2}, Lcom/kwad/sdk/api/loader/DFileLock$2;-><init>(Lcom/kwad/sdk/api/loader/DFileLock;Lcom/kwad/sdk/api/loader/DFileLock$LockListener;)V

    invoke-direct {p0, v0, v1, p1}, Lcom/kwad/sdk/api/loader/DFileLock;->lockFile(Ljava/io/File;ZLcom/kwad/sdk/api/loader/DFileLock$LockCallback;)V

    return-void
.end method
